<?php
include_once '../../../../mainfile.php';
ini_set('include_path', '../');

//include("mysql2json.class.php");

require_once 'Date/Date.php';
if(!isset($_SESSION['timestamp'])){
	$_SESSION['timestamp'] = mktime();
}

switch ($_GET['display'])//generate output according to selected tab
{
	case "day":
	if(isset($_GET['offset']) && $_GET['offset'] == '-1'){//PREVIOUS DAY
		$_SESSION['timestamp'] = $_SESSION['timestamp'] - 86400;
	}elseif(isset($_GET['offset']) && $_GET['offset'] == '1'){//NEXT DAY
		$_SESSION['timestamp'] = $_SESSION['timestamp'] + 86400;
	}elseif(isset($_GET['offset']) && $_GET['offset'] == 'undefined'){//TODAY
		$_SESSION['timestamp'] = mktime();
	}
	$json = "/*{'dateDisplay': '".date("F j, Y", $_SESSION['timestamp'])."'}*/";
	break;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
	case "week":
	if(isset($_GET['offset']) && $_GET['offset'] == '-1'){//PREVIOUS WEEK
			$month = Date_Calc::beginOfPrevWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%m');
			$day = Date_Calc::beginOfPrevWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%d');
			$year = Date_Calc::beginOfPrevWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%Y');
		$_SESSION['beginWeek'] = mktime(0,0,0,$month,$day,$year);
			$month = Date_Calc::endOfWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%m');
			$day = Date_Calc::endOfWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%d');
			$year = Date_Calc::endOfWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%Y');
		$_SESSION['endWeek'] = mktime(0,0,0,$month,$day,$year);
	}elseif(isset($_GET['offset']) && $_GET['offset'] == '1'){//NEXT WEEK
			$month = Date_Calc::beginOfNextWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%m');
			$day = Date_Calc::beginOfNextWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%d');
			$year = Date_Calc::beginOfNextWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%Y');
		$_SESSION['beginWeek'] = mktime(0,0,0,$month,$day,$year);
			$month = Date_Calc::endOfWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%m');
			$day = Date_Calc::endOfWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%d');
			$year = Date_Calc::endOfWeek(date("d",$_SESSION['beginWeek']),date("m",$_SESSION['beginWeek']),date("Y",$_SESSION['beginWeek']),$format='%Y');
		$_SESSION['endWeek'] = mktime(0,0,0,$month,$day,$year);
	}elseif(isset($_GET['offset']) && $_GET['offset'] == 'undefined'){//THIS WEEK
			$month = Date_Calc::beginOfWeek(date("d"),date("m"),date("Y"),$format='%m');
			$day = Date_Calc::beginOfWeek(date("d"),date("m"),date("Y"),$format='%d');
			$year = Date_Calc::beginOfWeek(date("d"),date("m"),date("Y"),$format='%Y');
		$_SESSION['beginWeek'] = mktime(0,0,0,$month,$day,$year);
			$month = Date_Calc::endOfWeek(date("d"),date("m"),date("Y"),$format='%m');
			$day = Date_Calc::endOfWeek(date("d"),date("m"),date("Y"),$format='%d');
			$year = Date_Calc::endOfWeek(date("d"),date("m"),date("Y"),$format='%Y');
		$_SESSION['endWeek'] = mktime(0,0,0,$month,$day,$year);
	}

	$json = "/*{'dateDisplay': '".date("M j",$_SESSION['beginWeek'])." - ".date("M j",$_SESSION['endWeek'])."'}*/";
	break;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
	case "month":
	if(isset($_GET['offset']) && $_GET['offset'] == '-1'){//PREVIOUS MONTH
			$month = Date_Calc::beginOfPrevMonth(date("d",$_SESSION['timestamp']),date("m",$_SESSION['timestamp']),date("Y",$_SESSION['timestamp']),$format='%m');
			$day = Date_Calc::beginOfPrevMonth(date("d",$_SESSION['timestamp']),date("m",$_SESSION['timestamp']),date("Y",$_SESSION['timestamp']),$format='%d');
			$year = Date_Calc::beginOfPrevMonth(date("d",$_SESSION['timestamp']),date("m",$_SESSION['timestamp']),date("Y",$_SESSION['timestamp']),$format='%Y');
		$_SESSION['timestamp'] = mktime(0,0,0,$month,$day,$year);
	}elseif(isset($_GET['offset']) && $_GET['offset'] == '1'){//NEXT MONTH
			$month = Date_Calc::beginOfNextMonth(date("d",$_SESSION['timestamp']),date("m",$_SESSION['timestamp']),date("Y",$_SESSION['timestamp']),$format='%m');
			$day = Date_Calc::beginOfNextMonth(date("d",$_SESSION['timestamp']),date("m",$_SESSION['timestamp']),date("Y",$_SESSION['timestamp']),$format='%d');
			$year = Date_Calc::beginOfNextMonth(date("d",$_SESSION['timestamp']),date("m",$_SESSION['timestamp']),date("Y",$_SESSION['timestamp']),$format='%Y');
		$_SESSION['timestamp'] = mktime(0,0,0,$month,$day,$year);
	}elseif(isset($_GET['offset']) && $_GET['offset'] == 'undefined'){//TODAY
		$_SESSION['timestamp'] = mktime(0,0,0,date("m"),1,date("Y"));
	}
	$json = "/*{'dateDisplay': '".date("M Y",$_SESSION['timestamp'])."'}*/";
	break;
}
//header('Content-type: text/html; charset=ISO-8859-1');
echo $json;
/*$num=0;
//any SQL select Query
$sql="select * from students s";
$result=@mysql_query($sql) or die(mysql_error());
$num=mysql_affected_rows();
//calling class
$objJSON=new mysql2json();
//print(trim($objJSON->getJSON($result,$num)));
 *
 * /*{ "cobblers": [
       {"filling": "peach", "timeToBake": "30" },
       {"filling": "cherry", "timeToBake": "35" },
       {"filling": "blueberry", "timeToBake": "30"}
    ],
"Pies": [
       {"filling": "peach", "timeToBake": "<?php echo $_GET['display'];?>" },
       {"filling": "cherry", "timeToBake": "35" },
       {"filling": "blueberry", "timeToBake": "30"}
    ]
}*/

/*{"dateDisplay": "09/02/1973"}*/
?>